home *** CD-ROM | disk | FTP | other *** search
- // Gmail Manager
- // By Todd Long <longfocus@gmail.com>
- // http://www.longfocus.com/firefox/gmanager/
-
- const GM_CC = Components.classes;
- const GM_CI = Components.interfaces;
-
- function gmParser() {}
- gmParser.prototype = {
- _console: null,
- _manager: null,
- _converter: null,
- _domParser: null,
- _domSerializer: null,
- _xpathEval: null,
- _doc: null,
-
- get doc() {
- if (!this._doc)
- this.load();
- return this._doc.documentElement;
- },
-
- get account() {
- if (!this._domParser)
- this.load();
- return this._domParser.parseFromString(
- ' <account type="" email="" alias="">\n' +
- ' <pref id="general-automatic-login" type="Bool" value="false"/>\n' +
- ' <pref id="general-secured-connection" type="Bool" value="true"/>\n' +
- ' <pref id="toolbar-account-hide-unread-count" type="Bool" value="false"/>\n' +
- ' <pref id="toolbar-account-hide-alias" type="Bool" value="false"/>\n' +
- ' <pref id="toolbar-tooltip-show-labels" type="Bool" value="true"/>\n' +
- ' <pref id="toolbar-tooltip-show-snippets" type="Bool" value="true"/>\n' +
- ' <pref id="toolbar-tooltip-show-accounts" type="Bool" value="false"/>\n' +
- ' <pref id="toolbar-unread-count-inbox" type="Bool" value="true"/>\n' +
- ' <pref id="toolbar-unread-count-spam" type="Bool" value="false"/>\n' +
- ' <pref id="toolbar-unread-count-labels" type="Bool" value="false"/>\n' +
- ' <pref id="notifications-check-interval" type="Int" value="15"/>\n' +
- ' <pref id="notifications-alerts-message-count" type="Bool" value="false"/>\n' +
- ' <pref id="notifications-alerts-snippets" type="Bool" value="false"/>\n' +
- ' <pref id="notifications-sounds" type="Bool" value="false"/>\n' +
- ' <pref id="notifications-sounds-file" type="Char" value=""/>\n' +
- ' </account>\n', "text/xml").documentElement;
- },
-
- load: function()
- {
- // Load services
- this._console = GM_CC['@mozilla.org/consoleservice;1'].getService(GM_CI.nsIConsoleService);
- this._manager = GM_CC["@longfocus.com/gmanager/manager;1"].getService(GM_CI.gmIManager);
- this._converter = GM_CC["@mozilla.org/intl/scriptableunicodeconverter"].createInstance(GM_CI.nsIScriptableUnicodeConverter);
- this._domParser = GM_CC['@mozilla.org/xmlextras/domparser;1'].getService(GM_CI.nsIDOMParser);
- this._domSerializer = GM_CC['@mozilla.org/xmlextras/xmlserializer;1'].getService(GM_CI.nsIDOMSerializer);
- this._xpathEval = GM_CC["@mozilla.org/dom/xpath-evaluator;1"].createInstance(GM_CI.nsIDOMXPathEvaluator);
-
- // Initialize converter
- this._converter.charset = "UTF-8";
-
- // Load preferences
- if (this._getPrefsFile("prefs.xml").exists())
- this.open(this._getPrefsFile("prefs.xml"));
-
- if (this._doc == null && this._getPrefsFile("prefs.bak").exists())
- this.open(this._getPrefsFile("prefs.bak"));
-
- if (this._doc == null)
- this._doc = this._defaultXML();
- },
-
- open: function(aFile)
- {
- var doc = this._readXML(aFile);
-
- if (doc != null)
- {
- // Transform prefs
- doc = this._transform(doc);
-
- // Validate prefs
- if (this._validate(doc))
- this._doc = doc;
- else
- doc = null;
- }
-
- return (doc != null);
- },
-
- save: function(aFile)
- {
- var file = (aFile != null ? aFile : this._getPrefsFile("prefs.xml"));
-
- // Backup prefs file
- if (aFile == null && this._getPrefsFile("prefs.xml").exists())
- this._getPrefsFile("prefs.xml").moveTo(null, "prefs.bak");
-
- this._writeXML(file, this._doc);
- },
-
- _readXML: function(aFile)
- {
- var doc = null;
- var fiStream = null;
- var siStream = null;
-
- try {
- var data = new String();
-
- fiStream = GM_CC["@mozilla.org/network/file-input-stream;1"].createInstance(GM_CI.nsIFileInputStream);
- siStream = GM_CC["@mozilla.org/scriptableinputstream;1"].createInstance(GM_CI.nsIScriptableInputStream);
-
- fiStream.init(aFile, 1, 0, false);
- siStream.init(fiStream);
-
- while (siStream.available() > 0)
- {
- var chunk = siStream.read(siStream.available());
- data += this._converter.ConvertToUnicode(chunk);
- }
-
- doc = this._domParser.parseFromString(data, "text/xml");
- }
- catch(e) {}
- finally {
- if (fiStream != null)
- fiStream.close();
- if (siStream != null)
- siStream.close();
- }
-
- return doc;
- },
-
- _writeXML: function(aFile, aDoc)
- {
- var content = this._domSerializer.serializeToString(aDoc);
- var foStream = null;
-
- try {
- // Create file out stream instance
- foStream = GM_CC["@mozilla.org/network/file-output-stream;1"].createInstance(GM_CI.nsIFileOutputStream);
-
- // Initialize file out stream to write
- foStream.init(aFile, 0x02 | 0x08 | 0x20, 0664, 0);
-
- // Write content to file
- var chunk = this._converter.ConvertFromUnicode(content);
- foStream.write(chunk, chunk.length);
-
- // Check if any content is left
- var fin = this._converter.Finish();
- if (fin.length > 0)
- foStream.write(fin, fin.length);
- }
- catch(e) {}
- finally {
- if (foStream != null)
- foStream.close();
- }
- },
-
- _defaultXML: function()
- {
- return this._domParser.parseFromString(
- '<?xml version="1.0"?>\n' +
- '<prefs version="' + this._manager.version + '">\n' +
- this._domSerializer.serializeToString(this._defaultGlobalXML()) +
- '</prefs>', "text/xml");
- },
-
- _defaultGlobalXML: function()
- {
- return this._domParser.parseFromString(
- ' <account type="global">\n' +
- ' <pref id="general-never-save-passwords" type="Bool" value="false"/>\n' +
- ' <pref id="compose-tab-location" type="List" value="2"/>\n' +
- ' <pref id="compose-mailto-links" type="Bool" value="false"/>\n' +
- ' <pref id="compose-mailto-default" type="List" value="0"/>\n' +
- ' <pref id="compose-context-menu" type="Bool" value="false"/>\n' +
- ' <pref id="compose-context-menu-mailto" type="Bool" value="false"/>\n' +
- ' <pref id="compose-context-menu-position" type="Int" value="0"/>\n' +
- ' <pref id="toolbar-left-click" type="List" value="7"/>\n' +
- ' <pref id="toolbar-middle-click" type="List" value="2"/>\n' +
- ' <pref id="toolbar-reset-unread-count" type="Bool" value="false"/>\n' +
- ' <pref id="toolbar-statusbar-display" type="Bool" value="true"/>\n' +
- ' <pref id="toolbar-statusbar-always-last" type="List" value="0"/>\n' +
- ' <pref id="toolbar-statusbar-position" type="Int" value="0"/>\n' +
- ' <pref id="notifications-switch-account" type="Bool" value="false"/>\n' +
- ' <pref id="notifications-clickable-alerts" type="Bool" value="true"/>\n' +
- ' </account>\n', "text/xml");
- },
-
- _getPrefsFile: function(aFilename)
- {
- var file = GM_CC["@mozilla.org/file/directory_service;1"].getService(GM_CI.nsIProperties).get("ProfD", GM_CI.nsIFile);
-
- // Append prefs directory
- file.append("gmanager");
-
- // Ensure directory
- if (!file.exists())
- file.create(file.DIRECTORY_TYPE, 0755);
-
- // Append filename
- file.append(aFilename)
-
- return file;
- },
-
- _transform: function(aDoc)
- {
- var processor = GM_CC["@mozilla.org/document-transformer;1?type=xslt"].createInstance(GM_CI.nsIXSLTProcessor);
- var transforms = GM_CC["@mozilla.org/file/directory_service;1"].getService(GM_CI.nsIProperties).get("ProfD", GM_CI.nsIFile);
- var oldVersion = aDoc.documentElement.getAttribute("version");
- var newVersion = this._manager.version;
- var doc = aDoc;
-
- if (oldVersion == null)
- oldVersion = "0.5";
-
- // Transforms directory
- //transforms.append("gmanager");
- transforms.append("extensions");
- transforms.append("{582195F5-92E7-40a0-A127-DB71295901D7}");
- transforms.append("defaults");
- transforms.append("transforms");
-
- while (oldVersion != newVersion && doc != null)
- {
- // Transform file
- var sheet = transforms.clone();
- sheet.append("prefs-" + oldVersion + ".xsl");
-
- try {
- // Process transform
- processor.reset();
- processor.importStylesheet(this._readXML(sheet));
- doc = processor.transformToDocument(doc);
-
- // Version of transform
- oldVersion = doc.documentElement.getAttribute("version");
- } catch(e) {
- // Something went wrong
- doc = null;
- }
- }
-
- return doc;
- },
-
- _validate: function(aDoc)
- {
- var defGlobal = this._defaultGlobalXML();
- var chkGlobal = this._evalPath(aDoc, "//account[@type = 'global']");
- var isValid = (chkGlobal != null);
-
- if (isValid)
- {
- var prefs = defGlobal.getElementsByTagName("pref");
-
- for (var i = 0; i < prefs.length && isValid; i++)
- {
- var id = ("@id='" + prefs.item(i).getAttribute("id") + "'");
- var type = ("@type='" + prefs.item(i).getAttribute("type") + "'");
- var expr = ("pref[" + id + " and " + type + "]");
- isValid = (this._evalPath(chkGlobal, expr) != null);
- }
- }
-
- return isValid;
- },
-
- _evalPath: function(aNode, aExpr)
- {
- var result = null;
-
- try {
- var results = this._xpathEval.evaluate(aExpr, aNode, null, GM_CI.nsIDOMXPathResult.ANY_TYPE, null);
- result = results.iterateNext();
- } catch(e) {}
-
- return result;
- },
-
- QueryInterface: function(iid)
- {
- if (!iid.equals(GM_CI.gmIParser) &&
- !iid.equals(GM_CI.nsISupports))
- throw Components.results.NS_ERROR_NO_INTERFACE;
- return this;
- }
- }
-
- var myModule = {
- firstTime: true,
-
- myCID: Components.ID("{d0fe9af0-f7bc-11da-974d-0800200c9a66}"),
- myDesc: "Preferences XML parser",
- myProgID: "@longfocus.com/gmanager/parser;1",
- myFactory: {
- createInstance: function (outer, iid) {
- if (outer != null)
- throw Components.results.NS_ERROR_NO_AGGREGATION;
-
- return (new gmParser()).QueryInterface(iid);
- }
- },
-
- registerSelf: function (compMgr, fileSpec, location, type)
- {
- if (this.firstTime) {
- this.firstTime = false;
- throw Components.results.NS_ERROR_FACTORY_REGISTER_AGAIN;
- }
-
- compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
- compMgr.registerFactoryLocation(this.myCID, this.myDesc, this.myProgID, fileSpec, location, type);
- },
-
- getClassObject: function (compMgr, cid, iid)
- {
- if (!cid.equals(this.myCID))
- throw Components.results.NS_ERROR_NO_INTERFACE;
-
- if (!iid.equals(Components.interfaces.nsIFactory))
- throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
-
- return this.myFactory;
- },
-
- canUnload: function(compMgr) { return true; }
- };
-
- function NSGetModule(compMgr, fileSpec) { return myModule; }